<?php

/*
 * This class is used for as Model for Loan table 
 */
class Model_Loan extends Model_Base {

    protected $_name = 'tbl_loan';
    protected $_primaryKey = 'ln_id';

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }
    
    //get customer data
    public function getcustomerArray() {
        $customerModel = new Model_Customer();
        $customerArray = $customerModel->fetchCustomer( "","","0",'cus_name','ASC');
        $finalArray[''] = 'Select Customer';
        foreach ($customerArray as $com) {
            $finalArray[$com['cus_id']] = $com['cus_name'] . " #" . $com['cus_actnumber'];
        }
        return $finalArray;
    }
    
    
    //get Agent data
    public function getagentArray() {
        $agentModel = new Model_Agent();
        $agentArray = $agentModel->fetchAgent( "","","0",'ag_name','ASC');
        $finalArray[''] = 'Select Agent';
        foreach ($agentArray as $com) {
            $finalArray[$com['ag_id']] = $com['ag_name'];
        }
        return $finalArray;
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('ln' => 'tbl_loan'))
                ->join(array('ag' => 'tbl_agent'), "ag.ag_id = ln.ln_ag_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ln.ln_cus_id")
                ->where('ln_id = ?', $id)
                ->where('ln.ln_status != 2');
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    
    /**get installment data**/
    public function getInstallmentData($loanid){
       
        $installmentModel = new Model_Installment();
        $where="ins_ln_id = ".$loanid;
        $installmentArray = $installmentModel->fetchInstallment($where, "", "0", 'ins_creditdate', 'ASC');
        if (!empty($installmentArray)) {
            return $installmentArray;
        } else {
            return array();
        }
        
    }
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('ln' => 'tbl_loan'))
                ->setIntegrityCheck(false)
                ->join(array('doc' => 'tbl_document'), "doc.doc_ln_id = ln.ln_id")
                ->where('ln_id = ?', $id)
                ->where('ln_status != 2');

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get loans
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchLoan($where = "", $limit = "", $offset = "0", $order = 'ln_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('ln' => 'tbl_loan'))
                ->setIntegrityCheck(false)
                ->join(array('ag' => 'tbl_agent'), "ag.ag_id = ln.ln_ag_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ln.ln_cus_id")
                ->where('ln_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
            $data = parent::fetchAll($select);
            if (!empty($data)) {
                return $data->toArray();
            } else {
                return array();
            }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getLoanCount($where) {
        $select = $this->select()
                ->from(array('ln' => 'tbl_loan'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('ag' => 'tbl_agent'), "ag.ag_id = ln.ln_ag_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ln.ln_cus_id")
                ->where('ln_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete loans
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteLoan($prdtArray) {
        $loginModel = new Model_Login();
        $config = Zend_Registry::get('config');
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_loan) {
                $loanData = Array();
                $loanData = $this->fetchEntryById($_loan);
                if (count($loanData) > 0) {
                    $data = array('ln_status' => '2');
                    $this->update($data, 'ln_id =' . (int) $_loan);
                    $documentModel=new Model_Document();
                    //delete folder
                    $where_doc="doc_ln_id = ".$_loan;
                    $documnetData = $documentModel->fetchDocument($where_doc);
                    $folder = $config->UPLOAD_PATH . "loan_document/";
                    $document_name = $folder . $documnetData[0]['doc_name'];
                    if (file_exists($document_name)) {
                        @unlink($document_name);
                    }
                    
                     //delete record from tbl_Document table
                     $documentModel->update(array('doc_status' => '2'), 'doc_ln_id =' . (int) $_loan);
                    
                }
            }
        } else {
            $ln_id = $prdtArray;
            $loanData = $this->fetchEntryById($ln_id);
            if (count($loanData) > 0) {
                $data = array('ln_status' => '2');
                $this->update($data, 'ln_id =' . (int) $ln_id);
                
                $documentModel=new Model_Document();
               //delete folder
                $where_doc="doc_ln_id = ".$ln_id;
                $documnetData = $documentModel->fetchDocument($where_doc);
                $folder = $config->UPLOAD_PATH . "loan_document/";
                $document_name = $folder . $documnetData[0]['doc_name'];
                if (file_exists($document_name)) {
                    @unlink($document_name);
                }
                
                 //delete record from tbl_Document table
                 $documentModel->update(array('doc_status' => '2'), 'doc_ln_id =' . (int) $ln_id);
                
            }
        }
    }

}
